package com.gxsf.back.controller;

import com.gxsf.back.config.ObjectType;
import com.gxsf.back.pojo.Admin;
import com.gxsf.back.pojo.Notice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.sql.ResultSet;
import java.util.Date;

/**
 * @author BeijiRonin
 * @date 2020/11/12
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/messy_json")
@Api("不重要的接口")
public class MessyController {

    @SneakyThrows
    @ApiOperation("上传图片到本地服务器")
    @PostMapping("/uploadPic.opera")
    public String upload(@RequestParam("avatar") MultipartFile multipartFile) {
        String[] split = multipartFile.getOriginalFilename().split("\\.");
        String savePath = ObjectType.SAVEPATH + new Date().getTime() + "." + split[split.length - 1];
        File file = new File(savePath);
        multipartFile.transferTo(file);
        log.info(savePath);
        return "https://api.yxing.site/messy_json/img?path=" + savePath;
    }
    @ApiOperation("返回本地图片，使用：http://localhost:4567/messy_json/img?path=???")
    @GetMapping(value = "/img", produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] test(@RequestParam String path) throws Exception {
        if (path.equals("undefined")) {
            return null;
        }
        File file = new File(path);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }
    @Data
    public static class NoticeResult {
        Notice notice;
        Admin admin;
    }
}
